package org.algorithm.Thread;

public class ThreadSolution2 {

   static Object lock = new Object();

    static int start = 0;
    static  int end = 100;
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(new Print(i)).start();
        }
    }

    static class Print implements Runnable {

        int index;

        public Print(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            while (start<end){
                synchronized (lock){
                    while (start%3!=index){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (start<end){
                        System.out.println(start+"---->"+Thread.currentThread().getId());
                        start++;
                    }
                    lock.notifyAll();
                }
            }
        }
    }

}
